package org.dromara.Nyt7InsuranceProduct.service.impl;

import cn.hutool.core.bean.BeanUtil;
import org.dromara.Nyt7InsuranceProduct.domain.Nyt7InsuranceProduct;
import org.dromara.Nyt7InsuranceProduct.domain.Nyt7InsuranceProduct1;
import org.dromara.Nyt7InsuranceProduct.domain.Nyt7InsuranceProduct2;
import org.dromara.Nyt7InsuranceProduct.mapper.Nyt7InsuranceProduct2Mapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.utils.utils.UserUtil;
import org.springframework.stereotype.Service;
import org.dromara.Nyt7InsuranceProduct.domain.bo.Nyt7InsuranceProductBo;
import org.dromara.Nyt7InsuranceProduct.domain.vo.Nyt7InsuranceProductVo;
import org.dromara.Nyt7InsuranceProduct.mapper.Nyt7InsuranceProductMapper;
import org.dromara.Nyt7InsuranceProduct.service.INyt7InsuranceProductService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 保险产品Service业务层处理
 *
 * @author yt
 * @date 2025-01-08
 */
@RequiredArgsConstructor
@Service
public class Nyt7InsuranceProductServiceImpl implements INyt7InsuranceProductService {

    private final Nyt7InsuranceProductMapper baseMapper;

    private final Nyt7InsuranceProduct2Mapper baseMapper2;

    private final UserUtil userUtil;

    /**
     * 查询保险产品
     *
     * @param id 主键
     * @return 保险产品
     */
    @Override
    public Nyt7InsuranceProduct queryById(Long id){
        Nyt7InsuranceProductVo p1 = baseMapper.selectVoById(id);
        Nyt7InsuranceProduct2 p2 = baseMapper2.selectByP1Id(p1.getId());
        Nyt7InsuranceProduct p = new Nyt7InsuranceProduct();
        BeanUtil.copyProperties(p1, p);
        BeanUtil.copyProperties(p2, p);
        return p;

    }

    /**
     * 分页查询保险产品列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 保险产品分页列表
     */
    @Override
    public TableDataInfo<Nyt7InsuranceProductVo> queryPageList(Nyt7InsuranceProductBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Nyt7InsuranceProduct1> lqw = buildQueryWrapper(bo);
        Page<Nyt7InsuranceProductVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的保险产品列表
     *
     * @param bo 查询条件
     * @return 保险产品列表
     */
    @Override
    public List<Nyt7InsuranceProductVo> queryList(Nyt7InsuranceProductBo bo) {
        LambdaQueryWrapper<Nyt7InsuranceProduct1> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Nyt7InsuranceProduct1> buildQueryWrapper(Nyt7InsuranceProductBo bo) {

        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Nyt7InsuranceProduct1> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCategoryName()), Nyt7InsuranceProduct1::getCategoryName, bo.getCategoryName());

        String postName = userUtil.getPostName();

        if(postName != null){
            lqw.eq(StringUtils.isNotBlank(postName), Nyt7InsuranceProduct1::getCompanyName, postName);
        }

        return lqw;
    }

    /**
     * 新增保险产品
     *
     * @param bo 保险产品
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(Nyt7InsuranceProductBo bo) {
        Nyt7InsuranceProduct1 add = MapstructUtils.convert(bo, Nyt7InsuranceProduct1.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
//        if (flag) {
//            bo.setId(add.getId());
//        }
        return flag;
    }

    /**
     * 修改保险产品
     *
     * @param bo 保险产品
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(Nyt7InsuranceProductBo bo) {
        Nyt7InsuranceProduct1 update = MapstructUtils.convert(bo, Nyt7InsuranceProduct1.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Nyt7InsuranceProduct1 entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除保险产品信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }



    /**
     * 获取保险产品总数
     */
    @Override
    public Long getCount() {
        return baseMapper.selectCount(null);
    }

    @Override
    public boolean saveBatch(List<Nyt7InsuranceProduct> list) {

        for (Nyt7InsuranceProduct l : list){
            Nyt7InsuranceProduct1 p1 = BeanUtil.copyProperties(l,Nyt7InsuranceProduct1.class);
            baseMapper.insertOne(p1);
            Nyt7InsuranceProduct2 p2 = BeanUtil.copyProperties(l,Nyt7InsuranceProduct2.class);
            p2.setProductId(p1.getId());
            baseMapper2.insert(p2);
        }

        return true;
    }

    /**
     * 根据ID列表查询保险产品列表
     *
     * @param ids ID列表
     * @return 保险产品列表
     */
    @Override
    public List<Nyt7InsuranceProductVo> queryListByIds(List<Long> ids) {
        LambdaQueryWrapper<Nyt7InsuranceProduct1> lqw = Wrappers.lambdaQuery();
        lqw.in(Nyt7InsuranceProduct1::getId, ids);
        return baseMapper.selectVoList(lqw);
    }


}
